<?php
class GoodsAction extends BaseAction
{
    public function __construct()
    {
		parent::__construct();
		$this->menuid = 5;
	}
	
    public function Index()
    {
        $goods_list = D("item")->order("id desc")->findAll();
		$this->assign("goods_list", $goods_list);
        $this->assign('sub_menu', $this->admin_menu($this->menuid));
        $this->display();
    }
    
	public function add()
	{
    	$result = D('cate')->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['cate_id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}
	    $this->assign('cate_list', $cate_list);
	    $brand_list = D('brand')->where('status=1')->findAll();
	    $this->assign('brand_list', $brand_list);
		$this->assign('sub_menu', $this->admin_menu($this->menuid));
		$this->display();
	}
	
    public function insert()
    {
        $model = D('item');
        $_POST['name'] = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : $this->error(L('input').L('goods_name'));
    	$_POST['goods_sn'] = isset($_POST['goods_sn']) && trim($_POST['goods_sn']) ? trim($_POST['goods_sn']) : $this->error(L('input').L('goods_sn'));
    	$_POST['cate_id'] = isset($_POST['cate_id']) && intval($_POST['cate_id']) ? intval($_POST['cate_id']) : $this->error(L('input').L('goods_cate'));
    	$_POST['brand_id'] = isset($_POST['brand_id']) && intval($_POST['brand_id']) ? intval($_POST['brand_id']) : $this->error(L('input').L('goods_brand'));
    	$_POST['sprice'] = isset($_POST['sprice']) && trim($_POST['sprice']) ? trim($_POST['sprice']) : $this->error(L('input').L('shop_price'));
    	$_POST['mprice'] = isset($_POST['mprice']) && trim($_POST['mprice']) ? trim($_POST['mprice']) : $this->error(L('input').L('market_price'));
    	$_POST['stock'] = isset($_POST['stock']) && intval($_POST['stock']) ? trim($_POST['stock']) : '999';
    	if (empty($_FILES['image']['name'])) {
            $this->error('请上传商品图片');
        } else {
            $this->_upload();
        }
        $_POST['add_time'] = time();
		if (false === $model->create()) {
			$this->error($model->getError());
		}
		//保存当前数据对象
		$goods_id = $model->add();
		$this->success(L('operation_success'));
    }
    
    function _upload()
    {
    	import("ORG.Util.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        $upload->savePath = './Public/Uploads/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        //$upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '448';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '336';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $_POST['img'] = $uploadList[0]['savename'];
        }
    }
    
    public function edit()
    {
        $goods_id = intval($_REQUEST['id']); //商品ID
        $model = D('item');
        $goods_info = $model->where('id='.$goods_id)->find();
        $this->assign('goods_info', $goods_info);
        $this->display();
    }
    
    public function update()
    {
        $goods_id = intval($_REQUEST['id']); //商品ID
		$model = D('item');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$model->where('id = '.$goods_id)->save();
    }
    
    public function delete()
    {
        $id = $_REQUEST['id'];
        $ids = explode ( ',', $id );
        foreach ($ids as $idd) {
            M("item")->where("id=".$idd)->delete();
        }
        $this->success ( L('DEL_SUCCESS') );
    }
}
?>